package com.sunshine.wrongquestionset.room.database

import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase

import com.sunshine.wrongquestionset.room.dao.*
import com.sunshine.wrongquestionset.room.entity.*

/**
 * @author sunshine
 * @date 2021/3/26
 */
@Database(
    entities = [
        AppConfigEntity::class,
        QuestionEntity::class,
        QuestionSetEntity::class,
        QuestionTypeEntity::class,
        SubjectEntity::class,
        UserEntity::class,
        WrongQuestionEntity::class,
        QuestionInfoEntity::class
    ],
    version = 1,
    exportSchema = false
)
abstract class MyDatabase: RoomDatabase() {
    abstract val appConfigDao: AppConfigDao
    abstract val questionDao: QuestionDao
    abstract val questionSetDao: QuestionSetDao
    abstract val questionTypeDao: QuestionTypeDao
    abstract val subjectDao: SubjectDao
    abstract val userDao: UserDao
    abstract val wrongQuestionDao: WrongQuestionDao
    abstract val questionInfoDao: QuestionInfoDao

    companion object {
        private var database: MyDatabase? = null

        @Synchronized
        fun getDatabase(context: Context): MyDatabase {
            if (database == null) {
                database = Room.databaseBuilder(context.applicationContext, MyDatabase::class.java, "database.db")
                    .allowMainThreadQueries()
                    .build()
            }
            return database!!
        }
    }
}